package com.scheuk.euler;

/**
 * A Pythagorean triplet is a set of three natural numbers, a  b  c, for which,
 * a^2 + b^2 = c^2
 * 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
 * There exists exactly one Pythagorean triplet for which a + b + c = 1000.
 * Find the product abc.
 * 
 * http://mathworld.wolfram.com/RightTriangle.html
 * For a right triangle with integer side lengths, any primitive Pythagorean
 * triple can be written as:
 * a = m^2 - n^2
 * b = 2mn
 * c = m^2 + n^2
 * where GCD(a,b,c) = 1 and m and n of different parity 
 * (one must be even, the other is even)
 * 
 * @author scheuk
 */
public class p009 {
	
	public static long PythagoreanTriplet(int value)
	{
		long a, b, c;
		
		long answer = 1;
		for (int m = 2; m < value; m+=2)
		{
			for (int n = 1; n < value; n+=2)
			{
				a = (m*m) - (n*n);
				b = 2*m*n;
				c = (m*m) + (n*n);
				if (a > 0 && b>0 && c>0)
				{
					long x = a+b+c;
					System.out.println(a + " " + b + " " + c + " = " + x);
					if (x == value) return (a*b*c);
				}
			}
		}
		return answer;
	}
	
	public static boolean isTriplet(int a, int b, int c)
	{
		boolean isValid = (((a*a) + (b*b)) == (c*c)) ? true : false;
		return isValid;
	}
	
	public static void main(String[] args)
	{
		System.out.println("ANSWER! " + PythagoreanTriplet(1000));
	}
}
